<?php
class PlansController extends AppController {

	var $name = 'Plans';
	function beforeFilter() {
		parent::beforeFilter();

		//$this->set('mod','galleries');
	}
	function index() {
		$this->paginate = array(
			'conditions'=>array('Plan.status' =>2),
			'limit'=>7,
			'order'=>'Plan.id DESC'
		);
		$this->set('galleries', $this->paginate());
	}
	function view($id = null){
		$this->Plan->recursive = 2;
		if (!$id) {
			$this->Session->setFlash(__('Không tồn tại bài viết này.', true));
			$this->redirect(array('action' => 'index'));
		}
		$conditions = array('Plan.status'=>2);
		$gal = $this->Plan->read(null, $id);
 		$this->set('gal',$gal);
		//debug($gal);
	}
	function admin_index() {
		$this->layout='admin';
		$this->Plan->recursive = 0;
		$conditions = '';
		$key = $this->data['Plan']['key'];
 		if($key!= null){
			$conditions = array('Plan.name LIKE '=>'%'.$key.'%');
		}else{
			$conditions = array();
		}
		$this->paginate = array(
			'conditions'=>$conditions,
			'order'=> 'Plan.id DESC'
		);
		$this->set(compact('key'));
		$this->set('galleries', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid Plan', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('gallery', $this->Plan->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Plan->create();
			if ($this->Plan->save($this->data)) {
				$this->Session->setFlash(__('Đã lưu !', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Có lỗi xảy ra. Hãy thử lại!', true));
			}
		}
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Không tồn tại thư viện ảnh này', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Plan->save($this->data)) {
				$this->Session->setFlash(__('Đã sửa', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('Chưa sủa được.Hãy thử lại.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Plan->read(null, $id);
		}
		//$this->set('model',$this->uses[0]);
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Plan', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Plan->delete($id)) {
			$this->Session->setFlash(__('Plan deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Plan was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
